package com.tencent.wxcloudrun.project.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tencent.wxcloudrun.common.Result;
import com.tencent.wxcloudrun.common.annotation.NotNUll;
import com.tencent.wxcloudrun.common.component.WeChatCommon;
import com.tencent.wxcloudrun.project.entity.*;
import com.tencent.wxcloudrun.project.entity.muti.VarietyMuti;
import com.tencent.wxcloudrun.project.entity.query.VarietyQuery;
import com.tencent.wxcloudrun.project.entity.query.WeChatQueryObject;
import com.tencent.wxcloudrun.project.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/unit")
public class UnitController {
    @Autowired
    private UnitService unitService;
    @Autowired
    private ClassificationService classificationService;
    @Autowired
    private TableNumService tableNumService;
    @Autowired
    private NormService normService;
    @Autowired
    private NormsService normsService;
    @Autowired
    private NewOrderService newOrderService;
    @Autowired
    public WeChatCommon weChatCommon;

//    @GetMapping("/test")
//    public Result  test(){
//        Order order=new Order();
//        order.setId(35L);
//        order.setRemarks("无");
//        order.setCustomId("oUdVz5fK0MgtlcLD7kQSrShrs2LY");
//        order.setTime(new Date());
//        BigDecimal bigDecimal=new BigDecimal("30.11");
//        order.setMoney(bigDecimal);
//        return Result.success(weChatCommon.sendSubscribe2(order));
//
//    }
    @GetMapping("/getNewOrder")
    public Result  getNewOrder(){
        return Result.success(newOrderService.getById(1));
    }

    @GetMapping("/addNewOrder")
    public Result  addNewOrder(){
        NewOrder newOrder= newOrderService.getById(1);
        newOrder.setNum(newOrder.getNum()+1);
        return Result.success(newOrderService.saveOrUpdate(newOrder));
    }

    @GetMapping("/getOpenId")
    public Result  getOpenId(@RequestParam @NotNUll String code) throws IOException {
        return Result.success(weChatCommon.getOpenid(code));
    }

    @GetMapping("/getNormsTerm")
    public Result  getNormsTerm(@RequestParam @NotNUll int normsType){
        QueryWrapper<Norms> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("norms_type",normsType);
        return Result.success(normsService.list(queryWrapper));
    }

    @GetMapping("/deleteNormsTerm")
    public Result  deleteNormsTerm(@RequestParam @NotNUll int id){
        return Result.success(normsService.removeById(id));
    }

    @GetMapping("/addNormsTerm")
    public Result  addNormsTerm(@RequestParam @NotNUll String name,@RequestParam @NotNUll Long normsType){
        Norms norms=new Norms();
        norms.setName(name);
        norms.setNormsType(normsType);
        return Result.success(normsService.save(norms));
    }

    @PostMapping("/addNorm")
    public Result  addNorm(@RequestBody Norm norm){
        return Result.success(normService.save(norm));
    }
    @GetMapping("/getNorm")
    public Result  getNorm(){
        return Result.success(normService.list());
    }
    @GetMapping("/getNormsWeApp")
    public Result  getNormsWeApp(@RequestParam @NotNUll Long normsType){
        JSONObject jsonObject = new JSONObject();
        Norm norm=normService.getById(normsType);
        QueryWrapper<Norms> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("norms_type",normsType);
        List<Norms> normsList=normsService.list(queryWrapper);
        jsonObject.set("name",norm.getName());
        jsonObject.set("list",normsList);
        return Result.success(jsonObject);
    }
    @GetMapping("/getPageNorm")
    public Result  getPageNorm(@RequestParam(defaultValue = "1") int pageNum,
                                         @RequestParam(defaultValue = "8") int pageSize){
        IPage<Norm> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Norm> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(normService.page(page,queryWrapper));
    }
    @PostMapping("/addClass")
    public Result  addClassification(@RequestBody Classification classification){
        return Result.success(classificationService.save(classification));
    }
    @GetMapping("/getClass")
    public Result  getClassification(){
//        WeChatQueryObject weChatQueryObject = new WeChatQueryObject();
//        weChatQueryObject.setPath("pages/regist/invitationUser?companyId=1");
//        weChatQueryObject.setWidth(340);
//        weChatQueryObject.setAuto_color(true);
//
        return Result.success(classificationService.list());
    }
    @GetMapping("/getPageClass")
    public Result  getPageClassification(@RequestParam(defaultValue = "1") int pageNum,
                                         @RequestParam(defaultValue = "8") int pageSize){
        IPage<Classification> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Classification> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(classificationService.page(page,queryWrapper));
    }
    @PostMapping("/addTable")
    public Result  addTableImg(@RequestBody WeChatQueryObject weChatQueryObject){
        return Result.success( weChatCommon.getWxCode(weChatQueryObject));
    }
    @GetMapping("/getPageTable")
    public Result  getPageTable(@RequestParam(defaultValue = "1") int pageNum,
                                         @RequestParam(defaultValue = "8") int pageSize){
        IPage<TableNum> page = new Page<>(pageNum,pageSize);
        QueryWrapper<TableNum> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(tableNumService.page(page,queryWrapper));
    }
    @GetMapping("/getTable")
    public Result  getTable(@RequestParam @NotNUll Long id){
        return Result.success(tableNumService.getById(id));
    }
    @GetMapping("/getUnit")
    public Result  getUnit(){
        return Result.success(unitService.list());
    }
    @PostMapping("/updateUnit")
    public Result  updateUnit(@RequestBody Unit unit){
        return Result.success(unitService.saveOrUpdate(unit));
    }
}
